﻿using System.Collections.Generic;
using DAL;

namespace BLL
{
    public abstract class BaseService<T> where T : class, new()
    {
        public DAL.BaseRepository<T> CurrentRepository { get; set; }

        //基类的构造函数
        public BaseService()
        {
            SetCurrentRepository();  //构造函数里面去调用了，此设置当前仓储的抽象方法
        }

        //约束
        public abstract void SetCurrentRepository();  //子类必须实现

        public bool Insert(T entity)
        {
            try
            {
                //调用T对应的仓储来做添加工作
                CurrentRepository.Insert(entity);
                return true;
            }
            catch
            {
                throw;
            }
        }

        public void Update(T entity)
        {
            CurrentRepository.Update(entity);
        }

        //T1 may be <int> or <string> or <object>

        public bool Delete<T1>(T1 Id)
        {
            try
            {
                CurrentRepository.Delete(Id);
                return true;
            }
            catch
            {
                throw;
            }
        }

        public T LoadEntity<T1>(T1 Id)
        {
            return CurrentRepository.LoadEntity(Id);
        }

        public IList<T> LoadEntities()
        {
            return CurrentRepository.LoadEntities();
        }
    }
}
